package com.sky.service.impl;

import com.sky.context.ThreadLocalUtil;
import com.sky.entity.AddressBook;
import com.sky.exception.BusinessException;
import com.sky.mapper.AddressMapper;
import com.sky.result.PageResult;
import com.sky.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<AddressBook> findList(AddressBook addressBook) {
       List<AddressBook> list = addressMapper.findList(addressBook);
        return list;
    }

    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(ThreadLocalUtil.getCurrentId());
        addressBook.setIsDefault(0);
        addressMapper.save(addressBook);
    }

    @Override
    public AddressBook findDefaultAddress() {
     return addressMapper.findDefaultAddress(ThreadLocalUtil.getCurrentId());
    }

    @Override
    public AddressBook findById(Long id) {
       AddressBook addressBook = addressMapper.findById(id);
       return addressBook;
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressMapper.updateAddressBook(addressBook);
    }

    @Override
    public void deleteById(Long id) {
        addressMapper.deleteById(id);
    }

    @Override
    public void setDefault(Long id) {
        addressMapper.setDisDefault(ThreadLocalUtil.getCurrentId());
        addressMapper.setDefault(id);
    }
}
